package agentV1

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"hc_server/pkg/app"
	"hc_server/routers/service/incomeService"
	"hc_server/runtime/e"
)

type GetIncomeInfoParam struct {
	Type any `json:"type"` //0=今日 1=昨日 2=本月 3=全部
}

func GetIncomeInfo(c *gin.Context) {
	appG := app.Gin{C: c}

	var (
		param   GetIncomeInfoParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	userId, _ := c.Get("userId")

	income := incomeService.Income{
		AgentId:  cast.ToInt(userId),
		TimeType: cast.ToInt(param.Type),
	}

	result, err := income.GetIncomeInfo()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}

type GetMerchantIncomeListParam struct {
	PageNum  any `json:"pageNum"`
	PageSize any `json:"pageSize"`
}

// GetMerchantIncomeList 获取收益商户列表
func GetMerchantIncomeList(c *gin.Context) {
	appG := app.Gin{C: c}

	var (
		param   GetMerchantIncomeListParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}
	userId, _ := c.Get("userId")

	income := incomeService.Income{
		AgentId:  cast.ToInt(userId),
		PageNum:  cast.ToInt(param.PageNum),
		PageSize: cast.ToInt(param.PageSize),
	}

	result, err := income.GetMerchantIncomeList()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}

type GetMerchantIncomeDateListParam struct {
	Time any `json:"time"`
}

func GetMerchantIncomeDateList(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   GetMerchantIncomeDateListParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}
	userId, _ := c.Get("userId")

	income := incomeService.Income{
		AgentId: cast.ToInt(userId),
		Time:    cast.ToString(param.Time),
	}

	result, err := income.GetMerchantIncomeDateList()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}

type GetMerchantIncomeDateInfoParam struct {
	Id any `json:"id"`
}

// GetMerchantIncomeDateInfo 收益商户订单详细
func GetMerchantIncomeDateInfo(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   GetMerchantIncomeDateInfoParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}
	userId, _ := c.Get("userId")

	income := incomeService.Income{
		AgentId: cast.ToInt(userId),
		OrderId: cast.ToInt(param.Id),
	}

	result, err := income.GetMerchantIncomeDateInfo()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}
